﻿#include "MiniFloatingWindow.h"
#include <iostream>
#include <QHBoxLayout>
#include <QIcon>
#include <QVBoxLayout>
#include <QLabel>
#include "ElaPushButton.h"
#include "ElaLineEdit.h"
#include "ElaComboBox.h"
#include "ElaScrollPageArea.h"
#include "ElaImageCard.h"
#include "ElaStatusBar.h"
#include "ElaText.h"
#include "GenerateReportBase.h"
#include "MainWindow.h"
#include "BaseModule/GlobalHelper.h"
#include "CreateNewReport.h"
using namespace std;
namespace GenerateReport
{
    MiniFloatingWindow::MiniFloatingWindow(QWidget* parent)
        : QWidget(parent)
    {
        //将浮标Widget设置为：顶层窗口；无标题栏；工具条模式；背景透明；无输入焦点
        setWindowFlags(Qt::FramelessWindowHint
            | Qt::WindowStaysOnTopHint | Qt::Tool);
        setAttribute(Qt::WA_TranslucentBackground, true);
        setFocusPolicy(Qt::NoFocus);
    }

    void MiniFloatingWindow::mousePressEvent(QMouseEvent* event)
    {
        //按住左键移动可以拖动窗口    
        if (event->button() == Qt::LeftButton)
        {
            m_CurrentPos = event->globalPos() - frameGeometry().topLeft();
            event->accept();
        }
    }

    void MiniFloatingWindow::mouseMoveEvent(QMouseEvent* event)
    {
        if (event->buttons() && Qt::LeftButton)
        {
            move(getFixGlobalPos(event->globalPos()));

            event->accept();
        }
    }

    void MiniFloatingWindow::paintEvent(QPaintEvent* event)
    {
        QPainter painter(this);
        painter.drawPixmap(0, 0, m_Pixmap);
    }

    void MiniFloatingWindow::leaveEvent(QEvent* event)
    {
        //鼠标离开窗口时是普通的指针    
        setCursor(Qt::ArrowCursor);
    }

    void MiniFloatingWindow::enterEvent(QEvent* event)
    {
        //鼠标留在窗口上时是一个手指    
        setCursor(Qt::PointingHandCursor);
    }

    void MiniFloatingWindow::mouseDoubleClickEvent(QMouseEvent* event)
    {
        GenerateReportBaseInstance->getMainWindow()->show();
        this->hide();
    }

    void MiniFloatingWindow::setMainWindow(QWidget* pMainWgt)
    {
        m_MainWindow = pMainWgt;
    }

    void MiniFloatingWindow::SetPixmap(const QPixmap& pixmap)
    {
        m_Pixmap = pixmap;
        update();
    }

    QPoint MiniFloatingWindow::getFixGlobalPos(QPoint mouseGPos)
    {
        QPoint mianGPoint = m_MainWindow->pos();
        int nMainLeft = mianGPoint.x();
        int nMainTop = mianGPoint.y();
        int nMainRight = mianGPoint.x() + m_MainWindow->width();
        int nMainBottom = mianGPoint.y() + m_MainWindow->height();

        int nPopLeft = mouseGPos.x() - this->width() / 2;
        int nPopTop = mouseGPos.y() - this->height() / 2;
        int nPopRight = mouseGPos.x() + this->width() / 2;
        int nPopBottom = mouseGPos.y() + this->height() / 2;

        // 最终要确定的只有左上角的坐标位置: 
        nPopLeft = nPopLeft < nMainLeft ? nMainLeft : nPopLeft;  // 弹窗左边超出主窗口，赋值为主窗口边界值。
        nPopTop = nPopTop < nMainTop ? nMainTop : nPopTop; //弹窗上部分超出主窗口上部值，赋值为主窗口上半边界值。
        nPopLeft = nPopRight > nMainRight ? nPopLeft - (nPopRight - nMainRight) : nPopLeft; //弹窗右侧超出主窗口右侧边界值，则弹窗左侧值向左移动超出的部分
        nPopTop = nPopBottom > nMainBottom ? nPopTop - (nPopBottom - nMainBottom) : nPopTop; //弹窗下侧超出主窗口下侧边界值，则弹窗上侧值向上移动超出的部分
        // 返回的QPoint是绝对坐标。
        return QPoint(nPopLeft, nPopTop);
    }

}
